'''
- [x] 完成了纯数字训练脚本的循行
- [ ] 验证训练脚本的正确性
'''

import tensorflow as tf
import numpy as np
import random
import os
import gymGUI as gymTTT
import ChessDQN
import threading
import time
from ChessDQNNoImg import ChessDQN

EPISODES = 5000

def trainThread():

    state = tttENV.resetRState()
    for e in range(EPISODES):
        tttENV.render()
        time.sleep(5)



if __name__ == "__main__":
    # chessDQN = ChessDQN.ChessDQN()

    # chessDQN.load('./data/weights')

    # for e in range(EPISODES):
    #     state = tttENV.reset()

    # tttENV.reset()

    # threading.Thread(target=trainThread).start()
    # tttENV.loop()

    tttENV = gymTTT.TTTGym()
    batch_size = 32
    chessDQN = ChessDQN()

    for e in range(EPISODES):
        state = tttENV.reset()
        state = np.reshape(state, [1, len(state)])

        for time in range(9):
            action = chessDQN(state)

            stepReturn = tttENV.step(action)

            chessDQN.remember(stepReturn[-1], stepReturn[1], stepReturn[2], stepReturn[3])

            if stepReturn[2] < 0:
                print("{} loop play chess exit".format(e))
                break

        if len(chessDQN.memory) > batch_size:
            chessDQN.replay(batch_size)
            if e % 10 == 0:
                chessDQN.save("./data/weights/ttt-dqn.h5")

